﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

    private string myControlCount;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Session.Count.ToString()] != null)
        {
            for (int i = 1; i <= Session.Count; i++)
            {
                MyControl myControl = LoadControl("~/MyControls/NewControl.ascx") as MyControl;
                myControl.ddlSelectedIndexChanged += new DropDownList1_SelectedIndexChanged(myControl_ddlSelectIndexChanged);
                Panel1.Controls.Add(myControl);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyControl myControl = LoadControl("~/MyControls/NewControl.ascx") as MyControl;
        // su dung event ddlSelectedIndexChanged dc khai bao ben NewControl
        myControl.ddlSelectedIndexChanged += myControl_ddlSelectIndexChanged;
        int i = 1;
        myControlCount = "Number" + i.ToString();
        Panel1.Controls.Add(myControl);
        Session.Add((Session.Count + 1).ToString(), myControl);
    }

    // viet ham thuc hien chuc nang selectindexchange cuar dropdownlist tai usercontrol
    void myControl_ddlSelectIndexChanged(object sender, EventArgs e)
    {
        MyControl myControl = (MyControl)(sender);
        if (myControl.ddlChoicePanel.SelectedIndex == 1)
        {
            myControl.multiView.SetActiveView(myControl.panel1);
        }
        else if (myControl.ddlChoicePanel.SelectedIndex == 2)
        {
            myControl.multiView.SetActiveView(myControl.panel2);
        }
        else if (myControl.ddlChoicePanel.SelectedIndex == 3)
        {
            myControl.multiView.SetActiveView(myControl.panel3);
        }
        else
        {
            myControl.multiView.SetActiveView(myControl.panel1);
        }
    }
}